
import { openInfo } from '@/utils/openInfo';
import { createRouter, createWebHistory } from 'vue-router'


//
const routes = [{
  path: '/',
  component: () => import("@/views/index.vue"),
  redirect: { name: 'Home' },
  children: [
    {
      //
      path: "Home",
      name: "Home",
      component: () => import('@/views/Home.vue')
    }, {
      // 用户个人中心
      path: "UserHome",
      name: "UserHome",
      component: () => import('@/views/user/UserHome.vue'),
      children: [
      ]
    }, {
      //考试
      path: "Chapter/:courseId",
      name: 'Chapter',
      props: true, //开启props路由传参数
      component: () => import('@/views/Chapter.vue')
    }, {
      // 教师中心
      path: "Teacher",
      name: "Teacher",
      component: () => import('@/views/teacher/TeacherIndex.vue'),
      redirect: { name: 'ClassMG' },
      children: [
        {
          //教师首页
          path: "TeacherHome",
          name: "TeacherHome",
          alias: "",
          component: () => import('@/views/teacher/TeacherHome.vue')
        }, {
          //班级管理
          path: "ClassMG",
          name: "ClassMG",
          component: () => import('@/views/teacher/ClassMG.vue')
        }, {
          //发送通知
          path: "ClassMG",
          name: "ClassMG",
          component: () => import('@/views/teacher/ClassMG.vue')
        }, {
          //发送通知
          path: "StudentMG",
          name: "StudentMG",
          component: () => import('@/views/teacher/StudentMG.vue')
        }, {
          //学习资料管理
          path: "MaterialMG",
          name: "MaterialMG",
          component: () => import('@/views/teacher/materialMG.vue')
        }, {
          //题库管理
          path: "TopicMG",
          name: "TopicMG",
          component: () => import('@/views/teacher/TopicMG.vue')
        }, {
          //考试管理
          path: "ExamMg",
          name: "ExamMg",
          component: () => import('@/views/teacher/ExamMg.vue')
        }, {
          //考试管理  
          path: "CorrectTestPaper",
          name: "CorrectTestPaper",
          component: () => import('@/views/teacher/CorrectTestPaper.vue')
        }, {
          //考试管理  
          path: "TeacherChat",
          name: "TeacherChat",
          component: () => import('@/views/teacher/TeacherChat.vue')
        }
      ]
    }, {
      // 学生主页
      path: "/Student",
      name: "Student",
      component: () => import('@/views/student/StudentIndex.vue'),
      redirect: { name: 'StudentHome' },
      children: [{
        // 学生首页
        path: "StudentHome",
        name: "StudentHome",
        component: () => import('@/views/student/StudentHome.vue'),
      }, {
        // 资料下载
        path: "MaterialDownload",
        name: "MaterialDownload",
        component: () => import('@/views/student/MaterialDownload.vue'),
      }, {
        // 我的测试
        path: "StudentExam",
        name: "StudentExam",
        component: () => import('@/views/student/StudentExam.vue'),
      }, {
        // 我的测试
        path: "Transcript",
        name: "Transcript",
        component: () => import('@/views/student/Transcript.vue'),
      }, {
        // StudentChat
        path: "StudentChat",
        name: "StudentChat",
        component: () => import('@/views/student/StudentChat.vue'),
      }
      ]
    }, {
      //课程管理
      path: "/CourseMgmt",
      name: "CourseMgmt",
      component: () => import('@/views/teacher/CourseMgmt.vue'),
    },
    {
      path: "/CourseEditor/:courseId",
      name: "CourseEditor",
      props: true,
      component: () => import('@/views/teacher/CourseEditor.vue'),
    }
  ]
}, {
  //考试
  path: "/Exam/:answerSheetId",
  name: 'Exam',
  props: true, //开启props路由传参数
  component: () => import('@/views/student/Exam.vue')
}, {
  //改卷
  path: "/CheckExam/:answerSheetId",
  name: 'CheckExam',
  props: true, //开启props路由传参数
  component: () => import('@/views/teacher/CheckExam.vue')
}, {
  //考试
  path: "/ExamDetails/:answerSheetId",
  name: 'ExamDetails',
  props: true, //开启props路由传参数
  component: () => import('@/views/student/ExamDetails.vue')
}, {
  //  
  path: "/JoinClass",
  name: "JoinClass",
  component: () => import('@/views/student/JoinClass.vue'),
}, {//测试页面
  path: "/Test",
  name: 'Test',
  component: () => import('@/views/Test.vue')
}, {
  path: "/Test1",
  name: 'Test1',
  component: () => import('@/views/Test1.vue')
}, {
  path: "/CreateExam",
  name: 'CreateExam',
  component: () => import('@/views/CreateExam.vue')
}, {
  path: "/ReadMd/:chapterId",
  name: "ReadMd",
  props: true, //开启props路由传参数
  component: () => import('@/views/ReadMd.vue')
}, {
  // 修改密码
  path: "/ChangePassword",
  name: "ChangePassword",
  component: () => import('@/views/user/ChangePassword.vue')
}, {
  // 用户登录注册
  path: "/LoginRegister",
  name: "LoginRegister",
  component: () => import('@/views/user/LoginRegister.vue')
}, {
  path: "/Admin",
  component: () => import("@/views/admin/AdminIndex.vue"),
  //  重定向到admin首页
  //redirect: {name:'AdminHome'},
  children: [
    {
      //管理员首页
      path: "AdminHome",
      name: "AdminHome",
      component: () => import('@/views/admin/AdminHome.vue')
    }, {
      //教师管理
      path: "TeacherMgmt",
      name: "TeacherMgmt",
      component: () => import('@/views/admin/TeacherMgmt.vue')
    }, {
      //发布公告
      path: "Announcement",
      name: "Announcement",
      component: () => import('@/views/admin/Announcement.vue')
    }, {
      //发布公告
      path: "AccountMgmt",
      name: "AccountMgmt",
      component: () => import('@/views/admin/AccountMgmt.vue')
    }
  ]
}, {
  //富文本编辑器
  path: "/MdEditor/:courseId/:chapterId",
  name: "MdEditor",
  props: true,
  component: () => import('@/views/MdEditor.vue')
}
  // ----------------------------------------
]


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const user = localStorage.getItem('user');
  const userInfo = user ? JSON.parse(user).userInfo : null;
  console.log(!userInfo);
  
  // 如果需要权限且未登录，重定向到登录页面
  if (!userInfo && to.name !== 'LoginRegister') {
    console.log(userInfo);
    openInfo("请先进行登录", "warning")
    next({ name: "LoginRegister" });
  }

  // else if(userInfo.role == "教师" && to.name == "Student"){
  //   console.log("老师去学生页面");

  //   next({ name: "Home" });
  // } 
  else {
    next(); // 否则继续导航
  }



});


// 全局后置守卫
// router.afterEach((to, from) => {
//   console.log(`后置路由守卫 from ${from.path} to ${to.path}`);
// });

export default router
